<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Company as Guestbooks;
use think\Db;

class Company extends Common
{
    private $cModel;   //当前控制器关联模型

    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Guestbooks;   //别名：避免与控制名冲突
    }

    public function index()
    {
        
        $where = [];
        if (input('get.search')){
           /// $where['uid|title|email'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());

        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function create()
    {
        //规模
        $people_id = Db ::name('company_people')->select();
        $this->assign('people_id', $people_id);
        // 融资
        $financing_id = Db ::name('company_financing')->select();
        $this->assign('financing_id', $financing_id);
        if (request()->isPost()){
            $data = input('post.');
            $pro = Db::name('province')->field('name')->where('province_id',$data['province'])->find();
            $cit = Db::name('city')->field('name')->where('city_id',$data['city'])->find();
            $dist = Db::name('district')->field('name')->where('district_id',$data['district'])->find();
            $data['site'] = implode(',',$pro).implode(',',$cit).implode(',',$dist);
            $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $province = Db::name('province') -> select();
            $people = Db::name('company_people') -> select();
            $financing = Db::name('company_financing') -> select();
            $trade = Db::name('company_trade') -> select();
            $this -> assign('trade',$trade);
            $this -> assign('financing',$financing);
            $this -> assign('province',$province);
            $this -> assign('people',$people);
            return $this->fetch('edit');
        }
    }


    public function edit($id)
    {
        //规模
        $people_id = Db ::table('company_people')->select();
        $this->assign('people_id', $people_id);
        // 融资
        $financing_id = Db ::name('company_financing')->select();
        $this->assign('financing_id', $financing_id);
        if (request()->isPost()){
            $data = input('post.');
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
            }else{
                $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $data = $this->cModel->get($id);
            ///$data->user;
            $this->assign('data', $data);
            $province = Db::name('province')->select();
            $city = Db::name('city')->where('province_id',$data['province'])->select();
            $district = Db::name('district')->where('city_id',$data['city'])->select();
            $people = Db::name('company_people')->select();
            $financing = Db::name('company_financing')->select();
            $trade =Db ::name('company_trade')->select();
            $this -> assign('trade',$trade);
            $this -> assign('financing',$financing);
            $this -> assign('people',$people);
            $this -> assign('city',$city);
            $this -> assign('province',$province);
            $this -> assign('district',$district);
            $this -> assign('data',$data);

            return $this->fetch();
        }
    }

    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                $where = [ 'id' => ['in', $id_arr] ];
                $result = $this->cModel->where($where)->delete();
                if ($result){
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            }
        }
    }
}

